package com.web.admin.web.cms.pan;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.State;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.admin.global.model.User;
import com.web.admin.global.util.HttpContext;
import com.web.cms.pan.exception.FolderException;
import com.web.cms.pan.facade.FileDataFacade;
import com.web.cms.pan.facade.FolderFacade;
import com.web.cms.pan.message.FolderFileResponse;
import com.web.cms.pan.message.FolderRequest;
import com.web.cms.pan.message.FolderResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 目录控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Controller("panFolderController")
@Http(url = "/cms/pan")
public class FolderController
{
	@Autowired
	private FolderFacade folderFacade;
	@Autowired
	private FileDataFacade fileDataFacade;
	@Resource
	private HttpContext httpContext;

	private static DictParser dictParser = DictParser.getInstance();
	
	public FolderController()
	{
	}

	@Http(url = "/folder_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		int parentId = request.getQueryInt("parentId", 0);
		List<FolderResponse> folderList = folderFacade.list(user.getId(), parentId, State.NORMAL);
		List<FolderResponse> pathList = folderFacade.listPath(user.getId(), parentId);
		Map<String, List<FolderResponse>> treeList = folderFacade.listTree(user.getId());
		List<FolderFileResponse> fileList = fileDataFacade.list(user.getId(), parentId, State.NORMAL);
		return View.createBuilder().setView("/cms/pan/folder_list")
						.put("parentId", parentId)
						.put("map", dictParser.getStringMap())
						.put("folderList", folderList)
						.put("pathList", pathList)
						.put("treeList", treeList)
						.put("fileList", fileList)
						.build();
	}

	@Http(url = "/folder_delete_list", method = HttpMethod.GET)
	public View deleteListGet(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		List<FolderResponse> folderList = folderFacade.list(user.getId(), null, State.DELETE);
		List<FolderFileResponse> fileList = fileDataFacade.list(user.getId(), -1, State.DELETE);
		return View.createBuilder().setView("/cms/pan/folder_delete_list")
						.put("map", dictParser.getStringMap())
						.put("folderList", folderList)
						.put("fileList", fileList)
						.build();
	}

	@Http(url = "/folder_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		FolderRequest folderRequest = request.getPostObject(FolderRequest.class);
		folderRequest.setUserId(user.getId());
		if (folderRequest.getId() == null)
		{
			ValidationUtil.valid(folderRequest, SaveModel.class);
			folderFacade.add(folderRequest);
		}
		else
		{
			ValidationUtil.valid(folderRequest, UpdateModel.class);
			folderFacade.update(folderRequest);
		}
	}

	@Http(url = "/folder_state", method = HttpMethod.POST)
	public void state(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		FolderRequest folderRequest = request.getPostObject(FolderRequest.class);
		folderRequest.setUserId(user.getId());
		folderFacade.state(folderRequest);
	}

	@Http(url = "/folder_operate", method = HttpMethod.POST)
	public void operate(HttpRequest request)
	{
		int srcId = request.getPostInt("srcId", 0);
		int destId = request.getPostInt("destId", 0);
		int type = request.getPostInt("type", 0);
		if (type == 0)
			throw new FolderException("未知操作");
		if (srcId == destId)
			throw new FolderException("源目录ID和目标目录ID不能相同");

		User user = httpContext.getUser(request);
		folderFacade.operate(user.getId(), srcId, destId, type);
	}

	@Http(url = "/folder_delete", method = HttpMethod.POST)
	public void delete(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		String ids = request.getPost("ids");
		folderFacade.delete(user.getId(), ids);
	}

}
